//package medicalApp.velociraptors.com;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Creates the dialog with the results for Patient Lookup
 * @author Vishnu
 *
 */
public class PatientLookupResults extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private String firstName;
	private String lastName;
	private String dateOfBirth;
	private int socialSN;

	private MedAppController mac;
	private JTable table;
	protected Integer chosenSSN;
	private JButton btnDeletePatient;
	private JButton btnCancel;

	
	/**
	 * Constructor for the Patient Lookup Results Dialog. 
	 * @param mac MedAppController mac
	 * @param drSSN int SSN for doctor
	 */
	public PatientLookupResults(final MedAppController mac, final int drSSN) {
		this.mac = mac;
		setBounds(100, 100, 800, 600);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 11, 770, 491);
			contentPanel.add(scrollPane);
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				/**
				 * click listener for table. 2 clicks opens dialog with view.
				 */
				public void mouseClicked(MouseEvent arg0) {
					if (arg0.getClickCount() == 2) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 3);
							DoctorPatientView dpv = new DoctorPatientView(mac, chosenSSN,drSSN);
							dpv.setVisible(true);
						}catch(Exception e){}
					}
					if (arg0.getClickCount() == 1) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 3);
						}catch(Exception e){}
					}
				}
			});
			table.setFillsViewportHeight(true);
			scrollPane.setViewportView(table);
		}

		btnDeletePatient = new JButton("Delete Selected Patient");
		btnDeletePatient.addActionListener(new ActionListener() {
			/**
			 * deletes selelected patient on button press
			 */
			public void actionPerformed(ActionEvent arg0) {
				if(chosenSSN!=null){
					int dialog = JOptionPane.showConfirmDialog(null, "Do you want to delete this patient?");
					if(dialog == JOptionPane.YES_OPTION){

						try {
							String sql = "DELETE FROM `patients` WHERE P_SSN = '" + chosenSSN + "'";
							mac.out.println(sql);
							String sql2 = "DELETE FROM `user` WHERE U_SSN = '" + chosenSSN + "'";
							mac.out.println(sql);
							Connection con = mac.getCon();
							Statement stmt = con.createStatement();
							int rs = stmt.executeUpdate(sql);
							int rs2 = stmt.executeUpdate(sql2);
							JOptionPane.showMessageDialog(null, "Patient Deleted!");
							mac.setDoctorPatientTableModel();
							table.setModel(myModel());
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				else
					JOptionPane.showMessageDialog(null, "Select a Patient!");
			}
		});
		btnDeletePatient.setBounds(20, 513, 180, 30);
		contentPanel.add(btnDeletePatient);

		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			/**
			 * closes the dialog
			 */
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancel.setBounds(600, 513, 180, 30);
		contentPanel.add(btnCancel);
	}

	/**
	 * Creates the table model for the table
	 * @return LookupTableModel model for the table
	 */
	public LookupTableModel myModel(){
		mac.out.println("LookupModel" + firstName + " " + lastName + " " + dateOfBirth + " " + socialSN);
		LookupTableModel model = new LookupTableModel(mac,firstName,lastName,dateOfBirth,socialSN);
		return model;
	}

	/**
	 * Allows setting of the table model by outside classes for live refresh
	 * @param model model to set the table to
	 */
	public void setTableModel(LookupTableModel model){
		table.setModel(model);
	}

	/**
	 * returns the first name
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * sets first name
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * returns last name
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * sets last name
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * returns date of birth
	 * @return the dateOfBirth
	 */
	public String getDateOfBirth() {
		return dateOfBirth;
	}

	/**
	 * sets date of birth
	 * @param dateOfBirth the dateOfBirth to set
	 */
	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	/**
	 * returns SSN
	 * @return the socialSN
	 */
	public int getSocialSN() {
		return socialSN;
	}

	/**
	 * sets SSN
	 * @param socialSN the socialSN to set
	 */
	public void setSocialSN(int socialSN) {
		this.socialSN = socialSN;
	}
}
